package it.canraid.leaflet.view;

import it.canraid.leaflet.control.Kernel;
import android.content.Context;
import android.graphics.Color;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.Transformation;
import android.widget.RelativeLayout;

public class TogglePanel extends RelativeLayout {
    private View mContent;
    private View mHandle;
    private Animation a = null;

    private volatile boolean mExpanded = false;
    private int mContentHeight = -1;
    private int mContentWidth = -1;

    public TogglePanel(Context context, View content) {
	super(context);
	this.mContent = content;

	RelativeLayout.LayoutParams fLP = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
	fLP.addRule(RelativeLayout.ALIGN_BOTTOM, 1);
	fLP.addRule(RelativeLayout.CENTER_HORIZONTAL, 1);
	addView(mContent, fLP);
	mHandle = new View(context);
	RelativeLayout.LayoutParams hLP = new RelativeLayout.LayoutParams(Kernel.WIDTH_BUTTON_TOGGLE_PANEL, Kernel.HEIGHT_BUTTON_TOGGLE_PANEL);
	hLP.addRule(RelativeLayout.ABOVE, mContent.getId());
	hLP.addRule(RelativeLayout.CENTER_HORIZONTAL, 1);
	addView(mHandle, hLP);
	mHandle.setBackgroundColor(Color.GREEN);
	mHandle.setOnClickListener(new OnClickListener() {
	    public void onClick(View v) {
		if (mExpanded)
		    collapse();
		else
		    expand();
	    }
	});
    }

    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
	if (mContentHeight == -1 || mContentWidth == -1) {
	    mContent.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
	    mContentWidth = mContent.getMeasuredWidth();
	    mContentHeight = mContent.getMeasuredHeight();
	    mExpanded = false;
	    android.view.ViewGroup.LayoutParams lp = mContent.getLayoutParams();
	    lp.width = 0;
	    lp.height = 0;
	    mContent.setLayoutParams(lp);
	}
	super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    public void expand() {
	if (mExpanded)
	    return;

	a = new ExpandAnimation(0, 1, true);
	a.setDuration(Kernel.TIME_ANIMATION_TOGGLE_PANEL);
	startAnimation(a);
    }

    public void collapse() {
	if (!mExpanded)
	    return;

	a = new ExpandAnimation(1, 0, false);
	a.setDuration(Kernel.TIME_ANIMATION_TOGGLE_PANEL);
	startAnimation(a);
    }

    private class ExpandAnimation extends Animation implements AnimationListener {
	private final int mStartHeight;
	private final int mDeltaHeight;
	private final int mStartWidth;
	private final int mDeltaWidth;
	private final boolean mExpectedExpandState;

	public ExpandAnimation(float start, float end, boolean expectedExpandState) {
	    mExpectedExpandState = expectedExpandState;
	    mStartHeight = Math.round(mContentHeight * start);
	    mDeltaHeight = Math.round((end - start) * mContentHeight);
	    mStartWidth = Math.round(mContentWidth * start);
	    mDeltaWidth = Math.round((end - start) * mContentWidth);
	    setAnimationListener(this);
	}

	protected void applyTransformation(float interpolatedTime, Transformation t) {
	    android.view.ViewGroup.LayoutParams lp = mContent.getLayoutParams();
	    lp.height = (int) (mStartHeight + mDeltaHeight * interpolatedTime);
	    lp.width = (int) (mStartWidth + mDeltaWidth * interpolatedTime);
	    mContent.setLayoutParams(lp);
	}

	public boolean willChangeBounds() {
	    return true;
	}

	public void onAnimationEnd(Animation animation) {
	    mExpanded = mExpectedExpandState;
	}

	public void onAnimationRepeat(Animation animation) {
	}

	public void onAnimationStart(Animation animation) {
	}

    }
}